package com.ruoyi.system.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.system.domain.HotspotPlatform;
import com.ruoyi.system.domain.MailCron;
import com.ruoyi.system.service.IHotspotPlatformService;
import com.ruoyi.system.service.IMailCronService;
import com.ruoyi.system.service.MailService;
import freemarker.template.Template;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * class
 *
 * @description:
 * @author: mabo
 * @date: 2022/1/29
 */
@Service
public class MailServiceImpl implements MailService {

    private static final Logger log = LoggerFactory.getLogger(MailServiceImpl.class);

    @Value("${spring.mail.username}")
    private String sendFrom;

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private FreeMarkerConfigurer configurer;

    @Autowired
    private IHotspotPlatformService iHotspotPlatformService;

    @Autowired
    private HotspotPlatform hotspotPlatform;

    @Autowired
    private IMailCronService iMailCronService;

    @Autowired
    private MailCron mailCron;

    @Override
    public void sendMessageMail() {
        List<HotspotPlatform> hotspotPlatformList = iHotspotPlatformService.selectHotspotPlatformList(hotspotPlatform);
        List<MailCron> mailCronList = iMailCronService.selectMailCronList(mailCron);
        for (MailCron mailCron : mailCronList) {
            try {
                MimeMessage mimeMessage = mailSender.createMimeMessage();
                MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
                helper.setFrom(sendFrom);
                helper.setTo(InternetAddress.parse(mailCron.getMailReceive()));
                helper.setSubject(mailCron.getMailTitle());
                //添加附件
                //FileSystemResource file = new FileSystemResource(new File());
                //helper.addAttachment("", file);
                //String json = JSONObject.toJSONString(hotspotPlatformList.get(0)).toString();
                Map<String, Object> model = new HashMap<>();
                model.put("params", hotspotPlatformList);
//                System.out.println(hotspotPlatformList.toString());
                Template template = configurer.getConfiguration().getTemplate(mailCron.getMailTemplatePath());
                String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
                helper.setText(text, true);
                mailSender.send(mimeMessage);
                log.info("发送邮件成功！");
            } catch (Exception e) {
                log.error("发送邮件失败！", e);
            }
        }
    }
}
